package com.ts.conch.admin.controller;

import com.ts.conch.admin.component.AutoLog;
import com.ts.conch.auth.base.BaseController;
import com.ts.conch.common.result.CommonResult;
import com.ts.conch.entity.TsAdmin;
import com.ts.conch.entity.TsMenu;
import com.ts.conch.entity.TsRole;
import com.ts.conch.service.ItsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: RoleController
 * @Decription: 此处填写类文件说明
 * @Author: IDai
 * @Date: 2021-09-13 15:52 星期一
 **/
@RestController
@RequestMapping(value = "/role")
@Api(tags = "[角色管理] - 控制层")
public class TsRoleController extends BaseController {

    @Autowired
    private ItsRoleService roleService;

    @ApiOperation("[角色管理] - 添加角色")
    @RequestMapping(value = "/create.ts", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", example = "管理员"),
            @ApiImplicitParam(name = "description", value = "角色描述", example = "此角色用来专注发票签收")
    })
    @AutoLog(value = "[角色管理] - 获取角色相关资源",operateType = "1")
    public CommonResult<?> create(@RequestBody TsRole role) {
        TsAdmin admin = getLoginUser();
        role.setEnterpriseId(admin.getEnterpriseId());
        return roleService.create(role);
    }

    @ApiOperation(value = "[角色管理] - 修改角色")
    @RequestMapping(value = "/update.ts", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", example = "1"),
            @ApiImplicitParam(name = "name", value = "角色名称", example = "管理员"),
            @ApiImplicitParam(name = "description", value = "角色描述", example = "此角色用来专注发票签收")
    })
    @AutoLog(value = "[角色管理] - 获取角色相关资源", operateType = "2")
    public CommonResult<?> update(@RequestBody TsRole role) {
        return roleService.update(role);
    }

    @ApiOperation(value = "[角色管理] - 删除指定角色")
    @RequestMapping(value = "/delete.ts", method = RequestMethod.POST)
    @ApiImplicitParam(name = "roleId", value = "角色id", example = "1")
    @AutoLog(value = "[角色管理] - 获取角色相关资源", operateType = "3")
    public CommonResult<?> delete(@RequestBody Map<String, Object> params) {
        String roleId = (String) params.get("roleId");
        return roleService.delete(roleId);
    }

    @ApiOperation(value = "[角色管理] - 获取企业下所有角色")
    @RequestMapping(value = "/listAll.ts", method = RequestMethod.POST)
    @AutoLog(value = "[角色管理] - 获取角色相关资源", operateType = "5")
    public CommonResult<?> listAll() {
        TsAdmin admin = getLoginUser();
        return roleService.list(admin.getEnterpriseId());
    }

    @ApiOperation("[角色管理] - 根据角色名称分页获取角色列表")
    @RequestMapping(value = "/list.ts", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "搜索框内容", example = "管理员"),
            @ApiImplicitParam(name = "page", value = "页码", example = "1"),
            @ApiImplicitParam(name = "size", value = "数量", example = "10")
    })
    @AutoLog(value = "[角色管理] - 获取角色相关资源", operateType = "5")
    public CommonResult<?> list(@RequestBody Map<String, Object> params) {
        TsAdmin admin = getLoginUser();
        params.put("enterpriseId", admin.getEnterpriseId());
        return roleService.list(params);
    }

    @ApiOperation(value = "[角色管理] - 修改角色状态")
    @RequestMapping(value = "/updateStatus.ts", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", example = "1"),
            @ApiImplicitParam(name = "status", value = "0:禁用,1:启用", example = "1")
    })
    @AutoLog(value = "[角色管理] - 获取角色相关资源", operateType = "2")
    public CommonResult<?> updateStatus(@RequestBody Map<String, Object> params) {
        return roleService.updateRoleStatus(params);
    }

    @ApiOperation(value = "[角色管理] - 获取角色相关菜单")
    @RequestMapping(value = "/listMenu.ts", method = RequestMethod.POST)
    @ApiImplicitParam(name = "roleId", value = "角色id", example = "1")
    @AutoLog(value = "[角色管理] - 获取角色相关资源")
    public CommonResult<List<TsMenu>> listMenu(@RequestBody Map<String, Object> params) {
        String roleId = (String) params.get("roleId");
        return roleService.listMenu(roleId);
    }

    @ApiOperation(value = "[角色管理] - 获取角色相关资源")
    @RequestMapping(value = "/listResource.ts", method = RequestMethod.POST)
    @ApiImplicitParam(name = "roleId", value = "角色id", example = "1")
    @AutoLog(value = "[角色管理] - 获取角色相关资源")
    public CommonResult<?> listResource(@RequestBody Map<String, Object> params) {
        String roleId = (String) params.get("roleId");
        return roleService.listPermission(roleId);
    }

    @ApiOperation(value = "[角色管理] - 给角色分配菜单")
    @RequestMapping(value = "/allocMenu.ts", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", example = "1"),
            @ApiImplicitParam(name = "menuIds", value = "菜单id列表", example = "1,2,3"),
    })
    @AutoLog(value = "[角色管理] - 给角色分配菜单")
    public CommonResult<?> allocMenu(@RequestBody Map<String, Object> params) {
        String roleId = (String) params.get("roleId");
        List<String> menuIdList = (List<String>) params.get("menuIds");
        return roleService.allocMenu(roleId, menuIdList);
    }

    @ApiOperation(value = "[角色管理] - 给角色分配资源")
    @RequestMapping(value = "/allocPermission.ts", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", example = "1"),
            @ApiImplicitParam(name = "permissionIds", value = "资源id列表", example = "1,2,3"),
    })
    @AutoLog(value = "[角色管理] - 给角色分配资源")
    public CommonResult<?> allocPermission(@RequestBody Map<String, Object> params) {
        String roleId = (String) params.get("roleId");
        List<String> permissionIdList = (List<String>) params.get("permissionIds");
        return roleService.allocPermission(roleId, permissionIdList);
    }
}
